import DtoContent from '../dto/DtoContent.js'
import DtoHeaders from '../dto/DtoHeaders.js'
import DtoCookies from '../dto/DtoCookies.js'

/**
 * Класс, описывающий структуру ответа
 */
export default class DtoResponse {
    /** @type {string} */
    statusText

    /** @type {string} */
    httpVersion

    /** @type {string} */
    redirectURL

    /** @type {number} */
    headersSize

    /** @type {number} */
    bodySize

    /** @type {number} */
    _transferSize

    /** @type {number} */
    status

    /** @type {object} */
    _error

    /** @type {DtoHeaders} */
    headers

    /** @type {DtoCookies} */
    cookies

    /** @type {DtoContent} */
    content

    constructor(obj, content) {
        this.statusText = obj?.statusText !== undefined ? obj.statusText : null
        this.httpVersion = obj?.httpVersion !== undefined ? obj.httpVersion : null
        this.redirectURL = obj?.redirectURL !== undefined ? obj.redirectURL : null
        this.headersSize = obj?.headersSize !== undefined ? obj.headersSize : null
        this.bodySize = obj?.bodySize !== undefined ? obj.bodySize : null
        this._transferSize = obj?._transferSize !== undefined ? obj._transferSize : null
        this.status = obj?.status !== undefined ? obj.status : null
        this._error = obj?._error !== undefined ? obj._error : null
        this.headers = obj?.headers !== undefined ? DtoHeaders.BuildFromObj(obj.headers) : null
        this.cookies = obj?.cookies !== undefined ? DtoCookies.BuildFromObj(obj.cookies) : null
        this.content = obj?.content !== undefined ? DtoContent.BuildFromObj(obj.content, content) : null
    }

    /**
     * Создать экземпляр класса из аналогичного по структуре объекта
     * @param {object} obj объект запроса
     * @param {string} content содержимое ответа
     * @returns экземпляр класса DtoResponse
     */
    static BuildFromObj(obj, content) {
        return new DtoResponse(obj, content)
    }

    /** @type {string} */
    getStatusText() {
        return this.statusText
    }

    /** @type {string} */
    getHttpVersion() {
        return this.httpVersion
    }

    /** @type {string} */
    getRedirectURL() {
        return this.redirectURL
    }

    /** @type {number} */
    getHeadersSize() {
        return this.headersSize
    }

    /** @type {number} */
    getBodySize() {
        return this.bodySize
    }

    /** @type {number} */
    getTransferSize() {
        return this._transferSize
    }

    /** @type {number} */
    getStatus() {
        return this.status
    }

    /** @type {object} */
    getError() {
        return this._error
    }

    /** @type {DtoHeaders} */
    getHeaders() {
        return this.headers
    }

    /** @type {DtoCookies} */
    getCookies() {
        return this.cookies
    }

    /** @type {DtoContent} */
    getContent() {
        return this.content
    }
}